<?php
/**
 * @copyright  Copyright  LexisNexis, a division of Reed Elsevier Inc. All rights reserved.
 */
namespace sdk;

include_once(__DIR__ . '/UsageClient.php');

class UsageApi {

    private static $obj;

    public static function instance() {
        if (!self::$obj) {
            self::$obj = new self();
        }
        return self::$obj;
    }

    private function __construct() {
        return;
    }

    public function listen() {
        $body = $this->getPost();
        $product = $body['product'];
        $name = $body['name'];
        $type = $body['type'];

        if (empty($product)) {
            die('The product name should not be empty.');
        }
        if (empty($name)) {
            die('The event name should not be empty.');
        }
        if (empty($type)) {
            die('The event type should not be empty.');
        }
        unset($body['product'], $body['name'], $body['type']);

        $url = $body['url'];
        if ($url) {
            $query = parse_url($url, PHP_URL_QUERY);
            if ($query) {
                $params = array();
                parse_str($query, $params);
                if (!empty($params['prid'])) {
                    $body['prid'] = $params['prid'];
                }
                if (!empty($params['crid'])) {
                    $body['crid'] = $params['crid'];
                }
            }
            $body['class'] = UsageClient::instance()->getClassName($url);
        }

        UsageClient::instance()->log($product, $name, $body, $type);
        die('Roger.');
    }

    private function getPost() {
        $body = array(
            'product'   => !empty($_POST['product'])    ? $_POST['product']     : '',
            'name'      => !empty($_POST['name'])       ? $_POST['name']        : '',
            'type'      => !empty($_POST['type'])       ? $_POST['type']        : '',
            'url'       => !empty($_POST['url'])        ? $_POST['url']         : '',
            'language'  => !empty($_POST['language'])   ? $_POST['language']    : '',
            'mouseX'    => !empty($_POST['mouseX'])     ? intval($_POST['mouseX'])      : 0,
            'mouseY'    => !empty($_POST['mouseY'])     ? intval($_POST['mouseY'])      : 0,
            'clientW'   => !empty($_POST['clientW'])    ? intval($_POST['clientW'])     : 0,
            'clientH'   => !empty($_POST['clientH'])    ? intval($_POST['clientH'])     : 0,
            'scrollTop' => !empty($_POST['scrollTop'])  ? intval($_POST['scrollTop'])   : 0,
            'documentW' => !empty($_POST['documentW'])  ? intval($_POST['documentW'])   : 0,
            'documentH' => !empty($_POST['documentH'])  ? intval($_POST['documentH'])   : 0,
        );

        $extra = array();
        foreach ($_POST as $key => $value) {
            if (substr($key, 0, 7) == '_event_' && strlen($key) > 7) {
                $k = substr($key, 7);
                if (isset($body[$k])) {
                    $extra[$key] = $value;
                } else {
                    $extra[$k] = $value;
                }
            }
        }

        return array_merge($body, $extra);
    }
}

UsageApi::instance()->listen();
